#include"Bsql.h"

extern char rootDir[64];
extern char database[64];

int getExpVal(int a, int b, char op){
    if(op=='+')
        return a + b;
    else if(op=='-')
        return a - b;
    else if(op=='*')
        return a * b;
    else
        return a / b;
}

void createDB(){
    chdir(rootDir);
    if(mkdir(database,S_IRUSR | S_IWUSR | S_IXUSR)==-1){
        printf("\nError!\n");
    }else{
        FILE *fileout = fopen("sys.dat", "a+");
        fprintf(fileout, "%s\n", database);
        fclose(fileout);
        chdir(database);
        FILE *f = fopen("sys.dat", "w");
        printf("\nCreate database %s successfully!\n", database);
        fclose(f);
    }
    strcpy(database, "\0");
    chdir(rootDir);
    printf("\nBSQL>");
}

void dropDB(char *dbname){
    chdir(rootDir);
    char cmd[128]="rm -rf ";
    FILE* filein;
    FILE* fileout;
    strcat(cmd, dbname);
    system(cmd);
    system("mv sys.dat sys.dat.tmp");
    filein = fopen("sys.dat.tmp", "r");
    fileout = fopen("sys.dat", "w");
    char dbnameTmp[64] = {0};
    while (fscanf(filein, "%s", dbnameTmp) != EOF){
        if(strcmp(dbnameTmp, database) != 0){
            fprintf(fileout, "%s\n", dbnameTmp);
        }
    }
    fclose(filein);
    fclose(fileout);
    system("rm sys.dat.tmp");
    printf("Drop database %s successfully!\n", dbname);
    printf("\nBSQL>");
}

void useDB(char *dbname){
    chdir(rootDir);
    if (chdir(dbname) == -1)
        printf("\nError!\n");
    else{
        strcpy(database, dbname);
        printf("Current Database: \n%s\n", database);
        chdir("rootDir");
    }
    printf("\nBSQL>");
}

void showDB(){
    chdir(rootDir);
    FILE *filein = fopen("sys.dat", "r");
    printf("databsaes:\n");
    char dbnameTmp[64]={0};
    while(fscanf(filein,"%s",dbnameTmp)!=EOF){
        printf("%s\n", dbnameTmp);
    }
    fclose(filein);
    chdir(rootDir);
    printf("\nBSQL>");
}

void createTable(char *tableval, struct hyper_items_def *Hitemroot){
    chdir(rootDir);
    if(strlen(database) == 0)
        printf("\nNo database, error!\n");
    else if(chdir(database) == -1)
        printf("\nError!\n");
    else if(access(tableval,F_OK)!=-1)
        printf("Table %s has existed!\n", tableval);
    else{
       FILE *fileout = fopen("sys.dat", "a+");
       fprintf(fileout, "%s\n", tableval);
       fclose(fileout);
       FILE *fileout_table = fopen(tableval, "w");
       int totRows = 0, i = 0;
       char Rows[64][64] = {0};
       struct hyper_items_def *HitemTmp = Hitemroot;
       while(HitemTmp!=NULL){
           strcpy(Rows[i], HitemTmp->field);
           i++;
           HitemTmp = HitemTmp->next;
       }
       totRows = i;
       fprintf(fileout, "%d\n", totRows);
       for (i = totRows - 1; i >= 0; i--){
           fprintf(fileout, "%s\n", Rows[i]);
       }
       fclose(fileout_table);
       //释放内存
       HitemTmp = Hitemroot;
       while(Hitemroot!=NULL){
           struct hyper_items_def *HitemTmp = Hitemroot;
           Hitemroot = HitemTmp->next;
           free(HitemTmp);
       }
       printf("CREATE TABLE %s SUCCESSFULLY!\n", tableval);
    }
    chdir(rootDir);
    printf("\nBSQL>");
}

void showTable(){
    chdir(rootDir);
    if(strlen(database) == 0)
        printf("\nNo database, error!\n");
    else if(chdir(database) == -1)
        printf("\nError!\n");
    else{
        FILE *filein = fopen("sys.dat", "r");
        char tableName[64] = {0};
        while(fscanf(filein,"%s",tableName)!=EOF){
            printf("%s\n", tableName);
        }
        fclose(filein);
    }
    chdir(rootDir);
    printf("\nBSQL>");
}

void dropTable(char *tableval){
    chdir(rootDir);
    if(strlen(database) == 0)
        printf("\nNo database, error!\n");
    else if(chdir(database) == -1)
        printf("\nError!\n");
    else if(access(tableval, F_OK) == -1)
        printf("Table %s doesn't exist!\n", tableval);
    else{
        char cmd[128]="rm -rf ";
        char tbname[64] = {0};
        FILE* filein;
        FILE* fileout;

        strcat(cmd, tableval);
        system(cmd);

        system("mv sys.dat sys.dat.tmp");
        filein = fopen("sys.dat.tmp", "r");
        fileout = fopen("sys.dat", "w");

        while(fscanf(filein, "%s", tbname) != EOF){
            if(strcmp(tbname, tableval) != 0){
                fprintf(fileout, "%s\n", tbname);
            }
        }
        fclose(filein);
        fclose(fileout);
        system("rm sys.dat.tmp");
    }
    chdir(rootDir);
    printf("Drop table %s succeessfully.\n", tableval);
    printf("\nBSQL>");
}

void multiInsert(char *tableval, struct item_def *itemroot, struct value_def *valroot){
    chdir(rootDir);
    if(strlen(database) == 0)
        printf("\nNo database, error!\n");
    else if(chdir(database) == -1)
        printf("\nError!\n");
    else if(access(tableval, F_OK) == -1)
        printf("Table %s doesn't exist!\n", tableval);
    else{
        char insertRows[64][64] = {0}, Rows[64][64] = {0};
        char values[64][64] = {0};
        int totRow = 0, i, j;
        FILE *filein = fopen(tableval, "r");
        fscanf(filein, "%d", &totRow);
        struct value_def *valueTmp = valroot;
        for(i=0;i<totRow;i++){
            fscanf(filein, "%s", Rows[i]);
            strcpy(values[i], valueTmp->value);//逆序
            valueTmp = valueTmp->next;
        }
        fclose(filein);
        if (itemroot == NULL){
            for (i = 0; i < totRow; i++){
                strcpy(insertRows[totRow-i-1], Rows[i]);
            }
        }
        else{
            i = 0;
            struct item_def *itemTmp = itemroot;
            while(itemTmp!=NULL){
                strcpy(insertRows[i], itemTmp->field);
                i++;
                itemTmp = itemTmp->next;
            }
        }
        FILE *fileout = fopen(tableval, "a+");
        for (i = totRow - 1; i >= 0; i--){
            for (j = 0; j < totRow; j++){
                if(strcmp(insertRows[i],Rows[j])==0){
                    fprintf(fileout, "%s\n", values[i]);
                }
            }
        }
        fclose(fileout);
        //释放空间
        while(itemroot!=NULL){
            struct item_def *itemTmp = itemroot;
            itemroot = itemTmp->next;
            free(itemTmp);
        }
        while(valroot!=NULL){
            struct value_def *valueTmp = valroot;
            valroot = valueTmp->next;
            free(valueTmp);
        }
        printf("Insert successfully!\n");
    }
    chdir(rootDir);
    printf("\nBSQL>");
}

int whereSearch(struct conditions_def *conRoot, int totField, char allField[][64], char value[][64]){
    if (conRoot->cmp_op == 7){//and
        return whereSearch(conRoot->left, totField, allField, value) && whereSearch(conRoot->right, totField, allField, value);
    }
    else if (conRoot->cmp_op == 8){//or
        return whereSearch(conRoot->left, totField, allField, value) || whereSearch(conRoot->right, totField, allField, value);
    }else{
        int field = 0;
        for (int i = 0; i < totField; ++i){
            if (strcmp(allField[i], conRoot->litem->field) == 0){
                field = i;
                break;
            }
        }
        if (conRoot->cmp_op == 1){
            if (strcmp(value[field], conRoot->value) == 0){
                return 1;
            }else
                return 0;
        }
        else if (conRoot->cmp_op == 2){
            if (conRoot->type == 1){
                return 0;
            }
            else if (atoi(value[field]) > atoi(conRoot->value)){
                return 1;
            }
            else
                return 0;
        }
        else if (conRoot->cmp_op == 3)
        {
            if (conRoot->type == 1){
                return 0;
            }else if (atoi(value[field]) < atoi(conRoot->value)){
                return 1;
            }else
                return 0;
        }
        else if (conRoot->cmp_op == 4)
        {
            if (conRoot->type == 1){
                return 0;
            }else if (atoi(value[field]) >= atoi(conRoot->value)){
                return 1;
            }else
                return 0;
        }
        else if (conRoot->cmp_op == 5)
        {
            if (conRoot->type == 1){
                return 0;
            }else if (atoi(value[field]) <= atoi(conRoot->value)){
                return 1;
            }else
                return 0;
        }
        else if (conRoot->cmp_op == 6)
        {
            if (strcmp(value[field], conRoot->value)==0){
                return 1;
            }else
                return 0;
        }
    }
}

void freeWhere(struct conditions_def *conRoot){
    if(conRoot!=NULL){
        freeWhere(conRoot->left);
        freeWhere(conRoot->right);
        free(conRoot);
    }
}

void deletes(char *tableval, struct conditions_def *conroot){
    chdir(rootDir);
    if(strlen(database) == 0)
        printf("\nNo database, error!\n");
    else if(chdir(database) == -1)
        printf("\nError!\n");
    else if(access(tableval, F_OK) == -1)
        printf("Table %s doesn't exist!\n", tableval);
    else{
        if(conroot==NULL){
            char Rows[64][64];
            FILE *filein;
            FILE *fileout;
            int totRows = 0, i = 0;
            filein = fopen(tableval, "r");
            fscanf(filein, "%d", &totRows);
            for (i = 0; i < totRows; i++){
                fscanf(filein, "%s", Rows[i]);
            }
            fclose(filein);

            fileout = fopen(tableval, "w");//原文件存在，会删除原有内容
            fprintf(fileout, "%d\n", totRows);
            for (i = 0; i < totRows; i++){
                fprintf(fileout, "%s\n", Rows[i]);
            }
            fclose(fileout);
        }else{
            FILE *filein, *fileout;
            char cp_cmd[64] = "cp ", rm_cmd[64] = "rm -rf ";
            char tableTmp[64] = {0};
            strcpy(tableTmp, tableval);
            strcat(tableTmp, ".tmp");
            strcat(cp_cmd, tableval);
            strcat(cp_cmd, " ");
            strcat(cp_cmd, tableTmp);
            system(cp_cmd);

            filein = fopen(tableTmp, "r");
            fileout = fopen(tableval, "w");

            char Rows[64][64];
            int totRows = 0, i = 0, j=0;
            fscanf(filein, "%d", &totRows);
            fprintf(fileout, "%d\n", totRows);
            for (i = 0; i < totRows; i++){
                fscanf(filein, "%s", Rows[i]);
                fprintf(fileout, "%s\n", Rows[i]);
            }
            char value[64][64] = {0};
            int end = 1;
            while (end) {
                int conditionFlag = 0;
                for (j = 0; j < totRows; j++){
                    if(fscanf(filein,"%s",value[j])==EOF){
                        end = 0;
                        break;
                    }
                }
                if(end!=0){
                    conditionFlag = whereSearch(conroot, totRows, Rows, value);
                    if(!conditionFlag){
                        for (j = 0; j < totRows; j++)
                            fprintf(fileout, "%s\n", value[j]);
                    }
                }
            }
            fclose(fileout);
            fclose(filein);
            strcat(rm_cmd, tableTmp);
            system(rm_cmd);
            freeWhere(conroot);
        }
        printf("Delete successfully!\n");
    }
    printf("\nBSQL>");
    chdir(rootDir);
}

void updates(char *tableval, struct upcon_def *uproot, struct conditions_def *conroot){
    chdir(rootDir);

    if(strlen(database) == 0)
        printf("\nNo database, error!\n");
    else if(chdir(database) == -1)
        printf("\nError!\n");
    else if(access(tableval,F_OK)==-1)
        printf("Table %s doesn't exist!\n", tableval);
    else{
        char Rows[64][64] = {0}, updateRows[64][64] = {0};
        int totRows = 0, totUpdateRows = 0, i, j, k;
        char updateValues[64][64];
        struct upcon_def *upTmp = uproot;
        i = 0;
        while(upTmp!=NULL){
            strcpy(updateRows[i], upTmp->field);
            strcpy(updateValues[i], upTmp->value);
            i++;
            upTmp = upTmp->next;
        }
        totUpdateRows = i;
        if (conroot == NULL){
            FILE *filein, *fileout;
            char cp_cmd[64] = "cp ";
            char rm_cmd[64] = "rm -rf ";
            char tableTmp[64] = {0};
            strcpy(tableTmp, tableval);
            strcat(tableTmp, ".tmp");
            strcat(cp_cmd, tableval);
            strcat(cp_cmd, " ");
            strcat(cp_cmd, tableTmp);
            strcat(rm_cmd, tableTmp);
            system(cp_cmd);

            filein = fopen(tableTmp, "r");
            fileout = fopen(tableval, "w");

            fscanf(filein, "%d", &totRows);
            fprintf(fileout, "%d\n", totRows);

            for (i = 0; i < totRows; i++){
                fscanf(filein, "%s", Rows[i]);
                fprintf(fileout, "%s\n", Rows[i]);
            }

            char value[64][64] = {0};
            int end = 0;
            while(!end){
                for (i = 0;i<totRows; i++){
                    if(fscanf(filein,"%s",value[i])==EOF){
                        end = 1;
                        break;
                    }
                }
                if(!end){
                    for (j = 0; j < totRows; j++){
                        for (k = 0; k < totUpdateRows; k++){
                            if(strcmp(Rows[j],updateRows[k])==0){
                                strcpy(value[j], updateValues[k]);
                                break;
                            }
                        }
                    }
                    for (j = 0; j < totRows; j++){
                        fprintf(fileout, "%s\n", value[j]);
                    }
                }
            }
            fclose(filein);
            fclose(fileout);
            system(rm_cmd);
            //释放内存
            while(uproot!=NULL){
                struct upcon_def* upTmp = uproot;
                uproot = upTmp->next;
                free(upTmp);
            }
        }else{
            FILE *filein, *fileout;
            char cp_cmd[64] = "cp ";
            char rm_cmd[64] = "rm -rf ";
            char tableTmp[64] = {0};
            strcpy(tableTmp, tableval);
            strcat(tableTmp, ".tmp");
            strcat(cp_cmd, tableval);
            strcat(cp_cmd, " ");
            strcat(cp_cmd, tableTmp);
            strcat(rm_cmd, tableTmp);
            system(cp_cmd);

            filein = fopen(tableTmp, "r");
            fileout = fopen(tableval, "w");

            fscanf(filein, "%d", &totRows);
            fprintf(fileout, "%d\n", totRows);

            for (i = 0; i < totRows; i++){
                fscanf(filein, "%s", Rows[i]);
                fprintf(fileout, "%s\n", Rows[i]);
            }

            char value[64][64] = {0};
            int end = 0;
            while(!end){
                for (i = 0;i<totRows; i++){
                    if(fscanf(filein,"%s",value[i])==EOF){
                        end = 1;
                        break;
                    }
                }
                int conditionFlag = whereSearch(conroot, totRows, Rows, value);
                if (!end){
                    if(conditionFlag){
                        for (j = 0; j < totRows; j++){
                            for (k = 0; k < totUpdateRows; k++){
                                if(strcmp(Rows[j],updateRows[k])==0){
                                    strcpy(value[j], updateValues[k]);
                                }
                            }
                        }
                        for (j = 0; j < totRows; j++){
                            fprintf(fileout, "%s\n", value[j]);
                        }
                    }else{
                        for (j = 0; j < totRows; j++){
                            fprintf(fileout, "%s\n", value[j]);
                        }
                    }
                }
            }
            fclose(filein);
            fclose(fileout);
            system(rm_cmd);
            //释放内存
            while(uproot!=NULL){
                struct upcon_def* upTmp = uproot;
                uproot = upTmp->next;
                free(upTmp);
            }
            freeWhere(conroot);
        }
    }
    printf("\nBSQL>");
    chdir(rootDir);
}

void selectWhere(struct item_def *itemroot, struct table_def *tableroot, struct conditions_def *conroot){
    chdir(rootDir);

    if(strlen(database) == 0)
        printf("\nNo database, error!\n");
    else if(chdir(database) == -1)
        printf("\nError!\n");
    else{
        char tableNames[64][64] = {0};
        int totTables = 0, i, j, k;
        struct table_def *tableTmp = tableroot;
        i = 0;
        while (tableTmp != NULL){
            strcpy(tableNames[i], tableTmp->table);
            i++;
            tableTmp = tableTmp->next;
        }
        totTables = i;
        for (i = 0; i < totTables; i++){
            if(access(tableNames[i],F_OK)==-1){
                printf("Table %s doesn't exit!\n", tableNames[i]);
                break;
            }
        }
        if(i<totTables){
            printf("\nBSQL>");
            return;
        }
        if(totTables==1){//单表
            if(itemroot==NULL && conroot==NULL){
                FILE *filein = fopen(tableNames[0], "r");
                int totRows = 0;
                fscanf(filein, "%d", &totRows);
                char value[64][64] = {0};
                int end = 0;
                while(!end){
                    for (i = 0; i < totRows; i++){
                        if(fscanf(filein, "%s", value[i])!=EOF)
                            printf("%*s|", 16, value[i]);
                        else{
                            end = 1;
                            break;
                        }
                    }
                    printf("\n");
                }
                fclose(filein);
            }else if(itemroot!=NULL && conroot==NULL){
                char chooseRows[64][64] = {0};
                char Rows[64][64] = {0};
                int totChoose = 0;
                struct item_def *itemTmp = itemroot;
                i = 0;
                while (itemTmp != NULL){
                    strcpy(chooseRows[i], itemTmp->field);
                    itemTmp = itemTmp->next;
                    i++;
                }
                totChoose = i;
                for (i = totChoose - 1; i >= 0; i--){
                    printf("%*s|", 16, chooseRows[i]);
                }
                printf("\n");
                FILE *filein = fopen(tableNames[0], "r");
                int totRows = 0;
                fscanf(filein, "%d", &totRows);
                for (i = 0; i < totRows; i++){
                    fscanf(filein, "%s", Rows[i]);
                }
                char value[64][64] = {0};
                int end = 0;
                while(!end){
                    for (i = 0; i < totRows; i++){
                        if(fscanf(filein, "%s", value[i])==EOF){
                            end = 1;
                            break;
                        }
                    }
                    if(!end){
                        for (i = totChoose - 1; i >= 0; i--){
                            for (j = 0; j < totRows; j++){
                                if(strcmp(chooseRows[i],Rows[j])==0){
                                    printf("%*s|", 16, value[j]);
                                    break;
                                }
                            }
                        }
                        printf("\n");
                    }
                }
                fclose(filein);
                }else if(itemroot==NULL && conroot!=NULL){
                    char value[64][64] = {0};
                    int totRows = 0;
                    char Rows[64][64] = {0};
                    FILE *filein = fopen(tableNames[0], "r");
                    fscanf(filein, "%d", &totRows);
                    for (i = 0; i < totRows; i++){
                        fscanf(filein, "%s", Rows[i]);
                        printf("%*s|", 16, Rows[i]);
                    }
                    printf("\n");
                    int end = 0;
                    while(!end){
                        for (i = 0; i < totRows; i++){
                            if(fscanf(filein,"%s",value[i])==EOF){
                                end = 1;
                                break;
                            }
                        }
                        if(!end && whereSearch(conroot,totRows,Rows,value)){
                            for (i = 0; i < totRows; i++){
                                printf("%*s|", 16, value[i]);
                            }
                            printf("\n");
                        }
                    }
                    fclose(filein);
                }else{
                    char value[64][64] = {0};
                    int totRows = 0;
                    char Rows[64][64] = {0};
                    int totChoose = 0;
                    char chooseRows[64][64] = {0};
                    struct item_def *itemTmp = itemroot;
                    i = 0;
                    while (itemTmp != NULL){
                        strcpy(chooseRows[i], itemTmp->field);
                        itemTmp = itemTmp->next;
                        i++;
                    }
                    totChoose = i;
                    for (i = totChoose - 1; i >= 0; i--){
                        printf("%*s|", 16, chooseRows[i]);
                    }
                    printf("\n");
                    FILE *filein = fopen(tableNames[0], "r");
                    fscanf(filein, "%d", &totRows);
                    for (i = 0; i < totRows; i++){
                        fscanf(filein, "%s", Rows[i]);
                    }
                    int end = 0;
                    while(!end){
                        for (i = 0; i < totRows; i++){
                            if(fscanf(filein,"%s",value[i])==EOF){
                                end = 1;
                                break;
                            }
                        }
                        if(!end && whereSearch(conroot,totRows,Rows,value)){
                            for (i = totChoose - 1; i >= 0; i--){
                                for (j = 0; j < totRows; j++){
                                    if(strcmp(chooseRows[i],Rows[j])==0){
                                        printf("%*s|", 16, value[j]);
                                        break;
                                    }
                                }
                            }
                            printf("\n");
                        }
                    }
                    fclose(filein);
                }
            //释放内存
            while(itemroot!=NULL){
                struct item_def *itemTmp = itemroot;
                itemroot = itemTmp->next;
                free(itemTmp);
            }
            freeWhere(conroot);
            printf("\nBSQL>");
        }else{//多表（最多两个）
            if(itemroot==NULL && conroot==NULL){
                char Rows1[64][64] = {0}, Rows2[64][64] = {0};
                char value1[64][64][64] = {0}, value2[64][64][64] = {0};
                int totRows1 = 0, totRows2 = 0;
                int totValue1 = 0, totValue2 = 0;
                int i, j, k;
                FILE *filein1, *filein2;
                filein1 = fopen(tableNames[1], "r");
                filein2 = fopen(tableNames[0], "r");
                fscanf(filein1, "%d", &totRows1);
                fscanf(filein2, "%d", &totRows2);
                for (i = 0; i < totRows1; i++){
                    fscanf(filein1, "%s", Rows1[i]);
                    printf("%*s|", 16, Rows1[i]);
                }
                for (i = 0; i < totRows2; i++){
                    fscanf(filein2, "%s", Rows2[i]);
                    printf("%*s|", 16, Rows2[i]);
                }
                printf("\n");
                int end = 0;
                i = 0;
                while (!end){
                    for (j = 0; j < totRows1; j++){
                        if(fscanf(filein1, "%s", value1[i][j])==EOF){
                            end = 1;
                            break;
                        }
                    }
                    if(!end)
                        i++;
                }
                totValue1 = i;
                //printf("totValue1=%d\n", totValue1);
                i = 0;
                end = 0;
                while (!end)
                {
                    for (j = 0; j < totRows2; j++){
                        if(fscanf(filein2, "%s", value2[i][j])==EOF){
                            end = 1;
                            break;
                        }
                    }
                    if(!end)
                        i++;
                }
                totValue2 = i;
                for (i = 0; i < totValue1; i++){
                    for (j = 0; j < totValue2; j++){
                        for (k = 0; k < totRows1; k++){
                            printf("%*s|", 16, value1[i][k]);
                        }
                        for (int m = 0; m < totRows2; m++){
                            printf("%*s|", 16, value2[j][m]);
                        }
                        printf("\n");
                    }
                }
                fclose(filein1);
                fclose(filein2);
                printf("\nBSQL>");
            }
            else if (itemroot == NULL && conroot != NULL){
                char Rows1[64][64] = {0}, Rows2[64][64] = {0};
                char value1[64][64][64] = {0}, value2[64][64][64] = {0};
                int totRows1 = 0, totRows2 = 0;
                int totValue1 = 0, totValue2 = 0;
                int i, j, k;
                FILE *filein1, *filein2;
                filein1 = fopen(tableNames[1], "r");
                filein2 = fopen(tableNames[0], "r");
                fscanf(filein1, "%d", &totRows1);
                fscanf(filein2, "%d", &totRows2);
                for (i = 0; i < totRows1; i++){
                    fscanf(filein1, "%s", Rows1[i]);
                    printf("%*s|", 16, Rows1[i]);
                }
                for (i = 0; i < totRows2; i++){
                    fscanf(filein2, "%s", Rows2[i]);
                    printf("%*s|", 16, Rows2[i]);
                }
                printf("\n");
                int end = 0;
                i = 0;
                while (!end){
                    for (j = 0; j < totRows1; j++){
                        if(fscanf(filein1, "%s", value1[i][j])==EOF){
                            end = 1;
                            break;
                        }
                    }
                    if(!end)
                        i++;
                }
                totValue1 = i;
                i = 0;
                end = 0;
                while (!end)
                {
                    for (j = 0; j < totRows2; j++){
                        if(fscanf(filein2, "%s", value2[i][j])==EOF){
                            end = 1;
                            break;
                        }
                    }
                    if(!end)
                        i++;
                }
                totValue2 = i;
                char value[64][64] = {0};
                int totRows = totRows1 + totRows2;
                char Rows[64][64] = {0};
                for (i = 0; i < totRows1; i++){
                    strcpy(Rows[i], Rows1[i]);
                }
                int p = 0;
                for (i = 0; i < totRows2; i++)
                {
                    strcpy(Rows[totRows1 + i], Rows2[i]);
                }
                    for (i = 0; i < totValue1; i++)
                    {
                        for (j = 0; j < totValue2; j++)
                        {
                            for (k = 0; k < totRows1; k++)
                            {
                                strcpy(value[k], value1[i][k]);
                            }
                            for (int m = 0; m < totRows2; m++)
                            {
                                strcpy(value[k + m], value2[j][m]);
                            }
                            if(whereSearch(conroot,totRows,Rows,value)){
                                for (k = 0; k < totRows; k++){
                                    printf("%*s|", 16, value[k]);
                                }
                                printf("\n");
                            }
                        }
                    }
                fclose(filein1);
                fclose(filein2);
                freeWhere(conroot);
                printf("\nBSQL>");
            }
        }
    }
}